 // index.js
// 获取应用实例
const app = getApp()

const url = require('../../utils/url')
Page({
  data: {
    invitationUserId:'',//邀请人id
    // 轮播图
    bannerImageArr: [],
    
   
    myBookBagList: [], //我的书包
    groupBooksList: [], //群组书架
    publicBooksList: [], //公共书架
    notifyList:[],//公共显示 // 通知
  },


    // 跳转图书详情
    booksdetail(event) {
      var id = event.currentTarget.dataset.id
      wx.navigateTo({
        url: '/pages/booksdetail/booksdetail?id=' + id,
      })
    },

    // 放入新书
    ishowbig:function(){
      wx.navigateTo({
        url: '/pages/mybookbag/mybookbag',
      })
    },

  // 首页书籍数据
  homeBooksData: function() {
    const that = this;
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token': wx.getStorageSync("token"),
      },
      url: getApp().url.dev + '/api/book/homeBooksData',
      success(res) {
        console.log(res);
        if (res.data.code == 200) {
          that.setData({
            myBookBagList: res.data.myBookBag,
            groupBooksList: res.data.groupBooks,
            publicBooksList: res.data.publicBooks,
          })
        }
      }
    })
  },
  
  //轮播图点击事件
  swiperimageclick(e) {
    console.log(e.currentTarget.dataset.item);
  },

  // 借还日历
  borrowing: function() {
    wx.navigateTo({
      url: '/pages/borrowreturndate/borrowreturndate',
    })
  },
  // 借阅排行
  seniority: function() {
    wx.navigateTo({
      url: '/pages/borrowrank/borrowrank',
    })
  },
  // 我的群书架
  GroupOfbookshelf: function() {
    wx.navigateTo({
      url: '/pages/GroupOfbookshelf/GroupOfbookshelf',
    })
  },

  onLoad(options) {
    // 判断是不是邀请进来的
    if(options.userinfo != undefined){
      console.log(options.userinfo)
      this.setData({
        invitationUserId:options.userinfo
      })
    }
    // console.log()
    // 判断用户是否注册 /  登录返回token
    this.login()
    const that = this;
    this.carouselMap();
    
  },

  // 轮播图接口
  carouselMap: function() {
    const that = this;
    wx.request({
      url: getApp().url.dev + '/api/common/carouselMap',
      success(res) {
        that.setData({
          bannerImageArr: res.data.data
        })
      }
    })
  },

  onShow() {
    this.homeBooksData(); //// 首页书籍数据
    this.notifyDisplay();  //公告显示
  },

  // 公告显示
  notifyDisplay: function() {
    const that = this;
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token': wx.getStorageSync("token"),
      },
      url: getApp().url.dev + '/api/common/notifyDisplay',
      success(res) {
        if (res.data.code == 200) {
          that.setData({
            notifyList:res.data.data
          })
        }
      }
    })
  },



  // 默认进入首页请求wxlogin判断用户是否注册 
  login:function(){
    var that = this;
    wx:wx.login({
      success: (res) => {
        console.log(res);
        if(res.code){
          wx:wx.request({
            url:url.authorization,
            data: {
              code: res.code
            },
            header: {
              'content-type': "application/json",// 改变默认值为这个配置
            },
            method: 'get',
            dataType: 'json',
            responseType: 'text',
            success:function(res){
              console.log(res);
              
              if(res.data.token){
                wx.setStorageSync("token", res.data.token)
                if(res.data.code == 503){
                  wx.setStorageSync("token", res.data.token)
                  wx.switchTab({
                    url: '/pages/my/my'
                  })
                }
              }else{
                wx.navigateTo({
                  url: '/pages/login/login?invitationUserId='+ that.data.invitationUserId
                })
              }
             
              
              that.homeBooksData(); //// 首页书籍数据
            }
          })
        }
      },
      
    })
  },


  // 下拉刷新
  onPullDownRefresh() {
    this.login()
    this.homeBooksData();
    this.carouselMap();//轮播图
    this.notifyDisplay();  //公告显示
    wx.showNavigationBarLoading();
    setTimeout(() => {
      //隐藏导航条加载动画
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
    }, 2000)
  },

  //跳转通知页面
  notice() {
    wx.navigateTo({
      url: '/pages/notice/notice',
    })
  },

  // 跳转我的书包
  mybookbag() {
    wx.navigateTo({
      url: '/pages/mybookbag/mybookbag',
    })
  },
  // 跳转公共书架
  publicshelf() {
    wx.navigateTo({
      url: '/pages/publicshelf/publicshelf',
    })
  }
})